/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.students;

import au.edu.swin.unilearn.submission.Submission;
import au.edu.swin.unilearn.submission.SubmissionEntity;
import au.edu.swin.unilearn.tutorialGroup.TutorialGroup;
import au.edu.swin.unilearn.tutorialGroup.TutorialGroupEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author DidierLeung
 */
@Entity
@NamedQueries({
@NamedQuery(name = "findAllStudents", query = "SELECT se FROM StudentEntity se")
})
@Table(name = "STUDENTS")
public class StudentEntity implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    private String sims_id;
    @Column
    private String student_name;
    @Column
    private String sims_password;
    @OneToMany(mappedBy = "studentEntity")
    private List<SubmissionEntity> submissionEntityCollection;
    @ManyToMany(cascade=CascadeType.ALL)//(mappedBy = "studentEntityCollection")
    @JoinTable(name="TUTORIAL_CLASS")
    private List<TutorialGroupEntity> tutorialGroupEntityCollection;

    public StudentEntity()
    {
    }

    public StudentEntity(String id, String name, String password)
    {
        this();
        setSims_id(id);
        setStudent_name(name);
        setSims_password(password);
        submissionEntityCollection = new ArrayList();
        tutorialGroupEntityCollection = new ArrayList();
    }

    public String getSims_id()
    {
        return sims_id;
    }

    public void setSims_id(String sims_id)
    {
        this.sims_id = sims_id;
    }

    public String getStudent_name()
    {
        return student_name;
    }

    public void setStudent_name(String student_name)
    {
        this.student_name = student_name;
    }

    public String getSims_password()
    {
        return sims_password;
    }

    public void setSims_password(String sims_password)
    {
        this.sims_password = sims_password;
    }

    public List<SubmissionEntity> getSubmissionEntityCollection()
    {
        return submissionEntityCollection;
    }

    public void setSubmissionCollection(List<SubmissionEntity> submissionEntityCollection)
    {
        this.submissionEntityCollection = submissionEntityCollection;
    }

    public Student getStudentObject()
    {
        List<TutorialGroup> tutorialGroupCollection =  new ArrayList();
        for(int i=0; i<tutorialGroupEntityCollection.size(); i++)
        {
            TutorialGroupEntity tge = tutorialGroupEntityCollection.get(i);
            tutorialGroupCollection.add(tge.getTutorialGroupObject());
        }
        
        List<Submission> submissionCollection =  new ArrayList();
        System.out.println("SUBMISSION SIZE: " + submissionEntityCollection.size());
        for(int j=0; j<submissionEntityCollection.size(); j++)
        {
            SubmissionEntity tge = submissionEntityCollection.get(j);
            submissionCollection.add(tge.getSubmissionObject());
        }
        return new Student(sims_id, student_name, sims_password, 
                                tutorialGroupCollection, submissionCollection);
    }

    public void setStudentEntityData(Student s)
    {
        setSims_id(s.getId());
        setStudent_name(s.getName());
        setSims_password(s.getPassword());
    }

    @Override
    public int hashCode()
    {
        return sims_id.hashCode();
    }

    @Override
    public boolean equals(Object object)
    {
        StudentEntity other = (StudentEntity) object;
        if (this.sims_id.equals(other.getSims_id()))
        {
            return true;
        }
        return false;
    }

    @Override
    public String toString()
    {
        return "au.edu.swin.unilearn.students.StudentEntity[id=" + sims_id + "]";
    }

    public List<TutorialGroupEntity> getTutorialGroupEntityCollection()
    {
        return tutorialGroupEntityCollection;
    }

    public void setTutorialGroupEntityCollection(List<TutorialGroupEntity> tutorialGroupEntityCollection)
    {
        this.tutorialGroupEntityCollection = tutorialGroupEntityCollection;
    }
    
    public void enrolStudent(TutorialGroupEntity tge)
    {
        tutorialGroupEntityCollection.add(tge);
    }
    
    public void addSubmission(SubmissionEntity se)
    {
        submissionEntityCollection.add(se);
    }
}
